package lunadevs.luna.module.exploits;

import org.lwjgl.input.Keyboard;

import com.darkmagician6.eventapi.EventTarget;
import com.zCore.Core.zCore;

import lunadevs.luna.category.Category;
import lunadevs.luna.events.PacketRecieveEvent;
import lunadevs.luna.events.PacketSendEvent;
import lunadevs.luna.events.UpdateEvent;
import lunadevs.luna.main.Luna;
import lunadevs.luna.module.Module;
import lunadevs.luna.utils.ClientUtils;
import lunadevs.luna.utils.TimeHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.network.play.client.C03PacketPlayer;
import net.minecraft.world.WorldSettings.GameType;

public class zPackets extends Module {

	int packetsCount;
	int delay = 0;
	  private double start;
	  private int d;
	boolean ground = true;
	public static float speed = 1.0F;
	int clock;
	public static int movement;
	public static Boolean active;
	TimeHelper timer = new TimeHelper();

	public zPackets() {
		super("LessPackets", Keyboard.KEY_NONE, Category.EXPLOITS, false);
	}

	@Override
	public void onUpdate() {
		if (!this.isEnabled)
			return;
		
		if (active == true) {
			if (clock > 70) {

				if (active == true) {
					ClientUtils.packet(new C03PacketPlayer.C06PacketPlayerPosLook(
							ClientUtils.player().posX + ClientUtils.player().motionX,
							ClientUtils.player().posY
									+ (Minecraft.getMinecraft().gameSettings.keyBindJump.isKeyDown() ? 0.0622 : 0)
									- (Minecraft.getMinecraft().gameSettings.keyBindSneak.isKeyDown() ? 0.0622 : 0),
							ClientUtils.player().posZ + ClientUtils.player().motionZ, ClientUtils.player().rotationYaw,
							ClientUtils.player().rotationPitch, false));
					ClientUtils.packet(new C03PacketPlayer.C06PacketPlayerPosLook(
							ClientUtils.player().posX + ClientUtils.player().motionX,
							ClientUtils.player().posY + ClientUtils.player().motionY - 0.999f,
							ClientUtils.player().posZ + ClientUtils.player().motionZ, ClientUtils.player().rotationYaw,
							ClientUtils.player().rotationPitch, true));
				}
				clock = 0;
			}
			this.movement += 1;

			if (active == true) {
				if (clock > 70) {

					if (active == true) {
						mc.thePlayer.noClip = true;
					}
					clock = 0;
				}
				
				this.delay += 1;
			      if (this.delay > 145)
			      {
			    	  Luna.addChatMessage("Successfully reset all incoming packets!");
			        mc.thePlayer.noClip = true;
			        mc.thePlayer.noClip = false;
			        this.delay = 0;
			      }
			      
			      this.delay += 1;
			      if (this.delay > 55)
			      {
			      //\\  Luna.addChatMessage("Successfully reset all incoming packets!");
			        mc.thePlayer.noClip = true;
			        mc.thePlayer.noClip = false;
			        /** Used for clearing incoming packet queue/server packets [Down below] */
			        zCore.mc().getNetHandler().cleanupzPack();
			        
			        this.delay = 0;
			      }

					this.movement += 1;
						this.packetsCount = 0;
						
						 this.delay = 0;
					}
					if (mc.thePlayer.onGround == true) {
						mc.playerController.setGameType(GameType.CREATIVE);
						mc.playerController.setGameType(GameType.SURVIVAL);
						if (this.movement > 1 && mc.thePlayer.isMoving()) {
							mc.thePlayer.motionX *= 1.005D;
							mc.thePlayer.motionZ *= 1.005D;
							this.movement = 0;
						}
						super.onUpdate();
			}
		}
	}
	
	@EventTarget
	public void update(final UpdateEvent eventCan){
		if(mc.thePlayer.posY == -0.1f && mc.thePlayer.motionY == -0.f)
		eventCan.setCancelled(true);
	}

	@EventTarget
	public void onPacketRecieve(PacketRecieveEvent event) {
		if (((event.getPacket() instanceof C03PacketPlayer))) {
			event.setCancelled(true);
			event.setCancelled(false);
		}
	}

	@EventTarget
	public void onPacket(PacketSendEvent event) {
		if (((event.getPacket() instanceof C03PacketPlayer))
				|| ((event.getPacket() instanceof C03PacketPlayer.C04PacketPlayerPosition))) {
			if (ClientUtils.player().motionY > -0.45 || ClientUtils.player().motionX > -0.45
					|| ClientUtils.player().motionZ > -0.45) {

			}
			this.packetsCount += 1;
			if (mc.gameSettings.keyBindLeft.pressed || mc.gameSettings.keyBindRight.pressed
					|| mc.gameSettings.keyBindBack.pressed || mc.gameSettings.keyBindForward.pressed) {

				this.packetsCount = 0;
			}
		}
	}

	@EventTarget
	public void onSendPacket(PacketSendEvent event) {
		if (!this.isEnabled)
			return;
		if (((event.getPacket() instanceof C03PacketPlayer)) || ((event.getPacket() instanceof C03PacketPlayer.C04PacketPlayerPosition)))
	    {
	      this.packetsCount += 1;
	        event.setCancelled(true);
	        event.setCancelled(false);
	        this.packetsCount = 0;
	      }else
		if (((event.getPacket() instanceof C03PacketPlayer))
				|| ((event.getPacket() instanceof C03PacketPlayer.C04PacketPlayerPosition))) {
			this.packetsCount += 1;
			if (mc.gameSettings.keyBindLeft.pressed || mc.gameSettings.keyBindRight.pressed
					|| mc.gameSettings.keyBindBack.pressed || mc.gameSettings.keyBindForward.pressed) {

				this.packetsCount = 0;
			}
		}
	}

	@Override
	public void onEnable() {
		active = true;
		super.onEnable();
	}

	@Override
	public void onDisable() {
		mc.playerController.setGameType(GameType.SURVIVAL);
		active = false;
		super.onDisable();
	}

	@Override
	public String getValue() {
		return null;
	}

}
